package com.servlet;

import com.entity.Cuisine;
import com.mapper.CuisineMapper;
import com.service.CartService;
import com.service.CuisineService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.UUID;

public class CuisineController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String op=request.getParameter("op");
        if(op.equals("add")){
            // 设置字符编码
            request.setCharacterEncoding("UTF-8");
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            Cuisine cuisine = new Cuisine();
            HttpSession session = request.getSession();
            String storeId = (String) session.getAttribute("storeId");
            cuisine.setStoreId(storeId);
            if (isMultipart) {
                try {
                    // 创建FileItem工厂
                    DiskFileItemFactory factory = new DiskFileItemFactory();
                    // 创建文件上传处理器
                    ServletFileUpload upload = new ServletFileUpload(factory);
                    // 解析请求参数列表
                    List<FileItem> items = upload.parseRequest(request);
                    for (FileItem item : items) {
                        if (item.isFormField()) {
                            // 处理普通表单字段
                            String fieldName = item.getFieldName();
                            String fieldValue = item.getString("UTF-8");
                            if (fieldName.equals("name")) {
                                cuisine.setCuisineName(fieldValue);
                            }else if(fieldName.equals("cuisineId")){
                                cuisine.setCuisineId(fieldValue);
                            }else if(fieldName.equals("materials")){
                                cuisine.setMaterials(fieldValue);
                            }else if(fieldName.equals("price")){
                                double price = Double.parseDouble(fieldValue);
                                cuisine.setPrice(price);
                            }
                        } else {
                            // 处理文件上传字段
                            String fieldName = item.getFieldName();
                            String fileName = item.getName();
                            String photoUPL = "http://localhost:8080/img/" + fileName;
                            System.out.println(photoUPL);
                            cuisine.setPhotoURL(photoUPL);
                            //保存上传的文件到指定目录
                            File uploadedFile = new File("D:/IdeaProjects/untitled_food/src/main/webapp/img/", fileName);
                            item.write(uploadedFile);
                        }
                    }
                    CuisineService cuisineService = new CuisineService();
                    cuisineService.addCuisine(cuisine);
                    request.getRequestDispatcher("myCuisines.jsp").forward(request, response);
                } catch (Exception e) {
                    response.getWriter().println("上传失败：" + e.getMessage());
                }
            } else {
                response.getWriter().println("不是multipart/form-data格式的请求！");
            }
        }
        else if(op.equals("delete")){
            // 设置字符编码
            request.setCharacterEncoding("UTF-8");
            String cuisineId = (String) request.getParameter("cuisineId");
            CuisineService cuisineService=new CuisineService();
            System.out.println(cuisineId);
            cuisineService.deleteCuisine(cuisineId);
            request.getRequestDispatcher("myCuisines.jsp").forward(request, response);
        }
    }

}
